﻿using GeneralServices.ViewModel;
using System;
using System.Windows;
using System.Windows.Controls;

namespace GenericWin
{
    /// <summary>
    /// Control générique. 
    /// Chaque nouveau control doit étendre cette classe pour pouvoir profiter de la charte graphique.
    /// </summary>
    public class GenericUserControl : UserControl
    {
        #region Attributs
                /// <summary>
        /// 
        /// </summary>
        private ResourceDictionary rd;
        #endregion

        #region DependencyProperty
                /// <summary>
        /// 
        /// </summary>
        public ViewModelBase VM
        {
            get { return (ViewModelBase)GetValue(VMProperty); }
            set { SetValue(VMProperty, value); }
        }

        // Using a DependencyProperty as the backing store for VM.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty VMProperty =
            DependencyProperty.Register("VM", typeof(ViewModelBase), typeof(GenericUserControl), new PropertyMetadata(null, VMUpdated));

        private static void VMUpdated(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            GenericUserControl gen = d as GenericUserControl;
            ViewModelBase vm = e.NewValue as ViewModelBase;
            if(gen != null && vm != null)
                gen.DataContext = vm;
        }
        #endregion

        #region Constructeur
                /// <summary>
        /// 
        /// </summary>
        public GenericUserControl()
        {
            rd = new ResourceDictionary();
            rd.Source = new Uri("pack://application:,,,/GeneralServices;component/ChartGraphics/DictionnaireResourcesConstellium.xaml");
           
            this.Resources.MergedDictionaries.Add(rd);
        }

        #endregion

        #region Methodes
         //public virtual ViewModelBase ExposeViewModel() { return null; }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public virtual ViewModelBase ExposeViewModel() { return VM; }
        #endregion  
    }
}
